﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara_Data_Layer.Vara_Data_Audit;
using Vara_Data_Layer.Vara_Data_Asset;
using Xentrix;

namespace AssetTracking.User_Controls
{
    public partial class AuditModal : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
        }
        //Show a blank modal
        public void Show()
        {
            this.XRCXenAudit.Show();
            CalendarTextBox_Date_Completed.SelectedDate = DateTime.Now;
            TextBoxCompletedBy.Text = XentrixModules.Reusable.Username();
            TextBoxActions.Text = string.Empty;
            //DropDownListOutcomes.Attributes.Add("onchange", "javascript:dropDownListOutcomeChanged();");
            //PopulateAuditOutcomes();
                    }
        //Show a blank modal and pass the audit ID
        public void Show(int AuditId)
        {
            this.XRCXenAudit.Show();
            if (AuditId != 0)
            {
                IVara_Data_Audit AuditDetails = new Vara_Data_Audit_Controller().ReturnAudit(AuditId);
                if (AuditId > 0)
                {
                    CalendarTextBox_Date_Completed.SelectedDate = AuditDetails.Date;
                    TextBoxCompletedBy.Text = AuditDetails.CompletedBy;
                    TextBoxActions.Text = AuditDetails.Actions;
                }
                else
                {
                    CalendarTextBox_Date_Completed.SelectedDate = DateTime.Now;
                    TextBoxCompletedBy.Text = XentrixModules.Reusable.Username();
                    TextBoxActions.Text = string.Empty;
                }
            }
        }
        //Show a model and pass an asset and an audit
        public void Show(int AssetId, int AuditId) {

            this.XRCXenAudit.Show();
            HiddenAssetId.Value = AssetId.ToString();
            HiddenAuditId.Value = AuditId.ToString();
            if (AuditId > 0)
            {
                IVara_Data_Audit AuditDetails = new Vara_Data_Audit_Controller().ReturnAudit(AuditId);
                if (AuditDetails.ID > 0)
                {
                    CalendarTextBox_Date_Completed.SelectedDate = AuditDetails.Date;
                    TextBoxCompletedBy.Text = AuditDetails.CompletedBy;
                    TextBoxActions.Text = AuditDetails.Actions;
                    
                }
                else
                { 
                    //something here to say audit not found
                }
            }
            else
            {
                CalendarTextBox_Date_Completed.SelectedDate = DateTime.Now;
                TextBoxCompletedBy.Text = XentrixModules.Reusable.Username();
                TextBoxActions.Text = "";
            }
        }
        protected void XRCXenAudit_Closing(object sender, EventArgs e)
        {
            if (Closed != null)
            {
                Closed(this, e);
            }
        }
        public EventHandler Closed;
        //Insert the audit into the database
        protected void ButtonSaveAudit_Click(object sender, EventArgs e)
        {
            if (int.Parse(HiddenAuditId.Value.ToString()) > 0)
            {
                Vara_Data_Audit_Controller AuditController = new Vara_Data_Audit_Controller();
                IVara_Data_Audit AuditToUpdate = AuditController.ReturnAudit(int.Parse(HiddenAuditId.Value.ToString()));
                AuditToUpdate.Date = CalendarTextBox_Date_Completed.SelectedDate.Value;
                AuditToUpdate.CompletedBy = TextBoxCompletedBy.Text;
                AuditToUpdate.AssetID = int.Parse(HiddenAssetId.Value.ToString());
                AuditToUpdate.Outcome = DropDownListOutcomes.Value;
                AuditToUpdate.Actions = TextBoxActions.Text;
                AuditController.UpdateAudit(AuditToUpdate);
                this.XRCXenAudit.Hide();
            }
            else
            {
                Vara_Data_Audit_Controller AuditController = new Vara_Data_Audit_Controller();
                Vara_Data_Audit NewAudit = new Vara_Data_Audit();
                NewAudit.Date = CalendarTextBox_Date_Completed.SelectedDate.Value;
                NewAudit.CompletedBy = TextBoxCompletedBy.Text;
                NewAudit.AssetID = int.Parse(HiddenAssetId.Value.ToString());
                NewAudit.Outcome = DropDownListOutcomes.Value;
                NewAudit.Actions = TextBoxActions.Text;
                AuditController.InsertAudit(NewAudit);
                this.XRCXenAudit.Hide();
            }
       }
        //Hide the popup
        protected void Hide()
        {
            this.XRCXenAudit.Hide();
        }
    }
}